package com.sx.pipeline.ui.bind

import android.app.Application
import com.blankj.utilcode.util.ToastUtils
import com.sx.mvvmlibs.base.BaseViewModel
import com.sx.mvvmlibs.http.CommonObserver
import com.sx.mvvmlibs.utils.RxThreadHelper
import com.sx.pipeline.app.model.CYBaseViewModel
import com.sx.pipeline.repos.http.req.BindPhoneDTO
import com.sx.pipeline.repos.http.res.LoginVO

/**
 * @Author: YY
 * @Date: 2022/5/17
 * @Description: TODO
 */
class BindPhoneVM(app: Application): CYBaseViewModel(app) {

    /**
     * 绑定手机号
     */
    fun bindPhone(openid: String, phone: String, smsCode: String, type: Int) {
        val dto = BindPhoneDTO(openid, phone, smsCode, type)
        addSubscribe(mModel.bindPhone(dto)
            .compose(RxThreadHelper.rxSchedulerHelper())
            .subscribeWith(object : CommonObserver<LoginVO, BaseViewModel<*>>
                (this, true) {

                override fun onResult(result: LoginVO) {
                    if (result.isBind) {
                        mModel.setToken(result.token!!)
                        // 获取用户信息
                        getUserInfo()
                    }
                }

                override fun onFailed(code: Int, msg: String?) {
                    super.onFailed(code, msg)
                    ToastUtils.showShort("$msg")
                }
            })
        )
    }
}